from base import IView
from flickrmirror.controller import MirrorController
from flickrmirror.tools import *

class CLIView(IView):
    
    name = 'cli'
    
    def __init__(self):
        self.controller = MirrorController(self)
        self.menu()
        
    def menu(self):
        res = '0'
        
        while res not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
            print """
flickrmirror
-----------
backup location : %s
authenticated : %s
total photos : %s

Options
-----------
1 : set backup location(required)
2 : authenticate with flickr(required)
3 : load set information
4 : list managed sets
5 : filter set(s) -- seperate by comma, '*' filters everything out
6 : remove filtered sets(s) -- seperate by comma, '*' un-filters everything
7 : save settings
8 : *Run Mirror(this will retrieve any photos not currently on the file system)
9 : exit
            """ % (
                str(self.controller.backup_directory),
                self.controller.authenticator.authenticated(),
                self.controller.get_photo_count()
            )
        
            res = raw_input("Make your selection: ").strip()
        
        if res == '1':
            self.controller.backup_directory = get_directory(self)
            self.menu()
        elif res == '2':
            self.controller.authenticate()
            self.menu()
        elif res == '3':
            self.controller.load()
            self.controller.synchronize_server()
            self.menu()
        elif res == '4':
            if self.controller.mirror_model is None:
                print "sets not loaded yet..."
                self.menu()
            print "-----------\nManaged Sets"
            for s in self.controller.mirror_model.filtered_sets():
                print "Title: '%s', id: '%s', %i photos" % (s.title, s.id, s.number_of_photos_and_videos)
                
            self.menu()
        elif res == '5':
            set = self.prompt("Enter set name or id seperated by a comma: ")
            self.controller.filter_set(set)
            self.menu()
        elif res == '6':
            set = self.prompt("Enter set name or id seperated by a comma: ")
            self.controller.unfilter_set(set)
            self.menu()
        elif res == '7':
            self.controller.save()
            self.menu()
        elif res == '8':
            self.controller.mirror()
            self.menu()
        elif res == '9':
            return
        else:
            self.menu()
        
    def ready(self):
        return len(str(self.controller.backup_directory)) > 0 and \
            self.controller.authenticator.authenticated()
        
    def prompt(self, msg, result=True):
        return raw_input(msg).strip()
        
    def log(msg):
        pass
        
    def status(self, msg, nl=True):
        if nl:
            print msg
        else:
            print msg,